package cn.cjlmonster.demo02.ui;

import android.content.Intent;
import android.util.Log;
import android.view.animation.ScaleAnimation;

import androidx.annotation.NonNull;
import androidx.lifecycle.LifecycleOwner;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.chad.library.adapter.base.listener.OnItemDragListener;
import com.chad.library.adapter.base.module.DraggableModule;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import java.util.List;

import cn.cjlmonster.demo02.R;
import cn.cjlmonster.demo02.base.BaseAppCompatActivity;
import cn.cjlmonster.demo02.virtual.DF;
import cn.cjlmonster.demo02.widget.RandomText;

/**
 * cn.cjlmonster.demo02.ui
 *
 * @author cjl
 * @project Demo02
 * @date 2022/11/4 19:56
 */
public class RecyclerActivity extends BaseAppCompatActivity {

    private static final String TAG = "RecyclerView测试";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_recycler;
    }

    @Override
    protected boolean canBack() {
        return true;
    }

    private RecyclerView recyclerView;

    @Override
    protected void initView() {
        recyclerView = findViewById(R.id.recyclerView);
    }

    private MyAdapter adapter;

    @Override
    protected void initData() {
        adapter = new MyAdapter(this, DF.deviceList);
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void initEvents() {
        adapter.setOnItemClickListener((adapter, view, position) -> {
            Intent intent = new Intent(this, DeviceActivity.class);
            intent.putExtra("index", position);
            startActivity(intent);
        });
        adapter.getDraggableModule().setOnItemDragListener(new OnItemDragListener() {
            @Override
            public void onItemDragStart(RecyclerView.ViewHolder holder, int pos) {
                ScaleAnimation scaleAnimation = new ScaleAnimation(1f, 1.05f, 1f, 1.05f);
                scaleAnimation.setFillAfter(true);
                scaleAnimation.setDuration(200);
                holder.itemView.clearAnimation();
                holder.itemView.startAnimation(scaleAnimation);
            }

            @Override
            public void onItemDragMoving(RecyclerView.ViewHolder source, int from, RecyclerView.ViewHolder target, int to) {

            }

            @Override
            public void onItemDragEnd(RecyclerView.ViewHolder holder, int pos) {
                ScaleAnimation scaleAnimation = new ScaleAnimation(1.05f, 1f, 1.05f, 1f);
                scaleAnimation.setFillAfter(true);
                scaleAnimation.setDuration(100);
                holder.itemView.clearAnimation();
                holder.itemView.startAnimation(scaleAnimation);
            }
        });
    }

    private static class MyAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> implements DraggableModule {

        private final LifecycleOwner owner;

        public MyAdapter(LifecycleOwner owner, List<MultiItemEntity> data) {
            super(data);
            this.owner = owner;
            getDraggableModule().setDragEnabled(true);
            addItemType(0, R.layout.item_recycler_card);
            addItemType(1, R.layout.item_recycler_card);
            addItemType(2, R.layout.item_recycler_card);
            addItemType(3, R.layout.item_recycler_card);
            addItemType(5, R.layout.item_recycler_card);
        }

        @Override
        protected void convert(@NonNull BaseViewHolder holder, MultiItemEntity device) {
            Log.i(TAG, "convert: position = " + getItemPosition(device) + ", itemType = " + device.getItemType() + ", " + holder.hashCode());
            RandomText randomText = holder.getView(R.id.randomText);
            randomText.setDevice(owner, device);
        }
    }
}
